﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BattleCity.GUI
{
    class ChangeLevelScreen : Screen
    {
        int speed = 2;

        Texture2D _background;
        Vector2 _back1pos, _back2pos;
        SpriteFont _font;
        byte _levelNumber;
        int _screenSize;

        public event EventHandler Hided;

        public ChangeLevelScreen(Game game, Texture2D back, SpriteFont font, byte lNumber)
            : base(game)
        {
            _sprBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            _background = back;
            _font = font;
            _levelNumber = lNumber;
            _screenSize = (Game as BattleCityGame).GameScreenSize.Y;
            _back1pos = new Vector2(0, -_screenSize / 2);
            _back2pos = new Vector2(0, _screenSize);
        }
        void GetInput()
        {
            KeyboardState kbstate = Keyboard.GetState();
            if (kbstate.IsKeyDown(Keys.Space) && Hided != null)
            {
                this.Hide();
                Hided(this, new EventArgs());
            }
        }
        public override void Update(GameTime gameTime)
        {
            if (_back1pos.Y != 0 && this.Visible) 
            {
                _back2pos.Y -= speed;
                _back1pos.Y += speed;
            }
            GetInput();
        }
        public override void Draw(GameTime gameTime)
        {
            _sprBatch.Draw(_background, _back1pos, null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.3f);
            _sprBatch.Draw(_background, _back2pos, null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.3f);

            Vector2 textPos = new Vector2();
            textPos.X = (Game as BattleCityGame).GameScreenSize.X - _font.MeasureString("STAGE " + _levelNumber.ToString()).X;
            textPos.Y = (Game as BattleCityGame).GameScreenSize.Y - _font.MeasureString("STAGE " + _levelNumber.ToString()).Y;
            textPos /= 2;
            _sprBatch.DrawString(_font, "STAGE " + _levelNumber.ToString(), textPos, Color.Black, 0f, new Vector2(0, 0), 1f, SpriteEffects.None, 0.2f);
            
            base.Draw(gameTime);
        }
    }
}